package com.learning.batterySwapPro.service.impl;

import com.learning.batterySwapPro.common.exception.BatterySwapException;
import com.learning.batterySwapPro.common.exception.BusinessException;
import com.learning.batterySwapPro.common.response.ResultCode;
import com.learning.batterySwapPro.dao.UserMapper;
import com.learning.batterySwapPro.pojo.entity.User;
import com.learning.batterySwapPro.service.UserService;
import jdk.nashorn.internal.ir.RuntimeNode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * @author wangyunyun
 */

@Service
@Slf4j
public class UserServiceImpl implements UserService {

	@Autowired
	private UserMapper userMapper;
	@Override
	public Integer insert(User user) {
		return userMapper.insertSelective(user);
	}

	@Override
	public Integer delete(Long id) {
		return userMapper.deleteByPrimaryKey(id);
	}

	@Override
	public Integer update(User user) {
		return userMapper.updateByPrimaryKeySelective(user);
	}

	@Override
	public User findById(Long id) {
		if (Objects.isNull(id)) {
			throw new BusinessException(ResultCode.BAD_REQUEST, "查找用户id不能为空");
		}
		return userMapper.selectByPrimaryKey(id);
	}
}
